/* "Efficiency and water use: Dynamic effects of irrigation technology adoption"
by Micah Cameron-Harp and Nathan Hendricks

Code written by Micah Cameron-Harp
May 16th, 2024

This do file creates the TWFE and Chaisemartin & D'Haultfoueille (2021) estimates
	for the ATT and dynamic treatment effects displayed in Appendix D.
*/

*Define directories and set working directory
	/* NOTE - To replicate our results, you need to change the directory address in the next line */
global dr_root = "C:\Users\Micah\Dropbox\Irrigation technology transition\final revisions for conditional acceptance\replication materials"
global dr_data = "$dr_root\data"
global dr_output = "$dr_root\outputs"
global dr_output_main = "$dr_root\outputs\main_text"
global dr_output_app = "$dr_root\outputs\appendices"
global dr_output_log = "$dr_root\outputs\logs"
global dr_temp = "$dr_root\data\intermediate"
cd "$dr_temp"

********************************************************************************
********** Flood to Center Pivot or LEPA ***************************************
********************************************************************************
*Now do TWFE for ATT and event study but for the years where pretrend test is passed (1996-2005)
	import delimited using "${dr_temp}/flood_cporlepa_prepped.csv", clear
	keep if inrange(wua_year, 1996, 2005) // 94% of transitions occur by 2012, and att_gt estimates won't converge if we use after 2015

	putexcel set "$dr_output_app\app_twfe.xlsx", ///
		sheet("floodtocplepa_96_05_nyt") modify
	local i = 3
	foreach y in "af_used"  "acres_irr" "depth_applied" {
		putexcel A`i' = "`y'"
		putexcel B`i' = "effect_average"
		sum `y'
		local var_mean = r(mean)
			putexcel H`i' = `var_mean'
		xtreg `y' treat_status ///
			jan_april_mean_ppt may_sep_mean_ppt jan_april_mean_ET0_elev may_sep_mean_ET0_elev i.wua_year ///
			, fe vce(cluster WR_GROUP)
		putexcel C`i' = _b[treat_status]
		putexcel D`i' = _se[treat_status]
		local ++i
	}

	putexcel set "$dr_output_app\app_twfe_event_study.xlsx", ///
		sheet("floodtocplepa_96_05") modify
	local i = 0
	foreach y in "af_used"  "acres_irr" "depth_applied" {
		local starter = 2 + `i'*37
		local ender = `starter' + 23
		putexcel F`starter' = "`y'"
		sum `y'
		local var_mean = r(mean)
			putexcel G`starter' = `var_mean'
			qui eventdd `y' jan_april_mean_ppt may_sep_mean_ppt jan_april_mean_ET0_elev may_sep_mean_ET0_elev i.wua_year, timevar(t_to) method(fe, cluster(WR_GROUP)) graph_op(leg(off))		
		putexcel A`starter' = matrix(e(leads))
		putexcel A`ender' = matrix(e(lags))
		local ++i
	}


************************************DYNAMIC - flood to CP or LEPA *************************************
import delimited using "${dr_temp}/flood_cporlepa_prepped.csv", clear
//////////////////////////////////
///// DROP NEVER TREATED /////////
//////////////////////////////////
keep if num_switch_flood_cplepa==1 /* May be unnecessary */

xtile init_af_quint = init_af_used, nq(5)
cd "$dr_output_tables"


foreach y in "af_used"  "acres_irr" "depth_applied" {
	did_multiplegt `y' wr_group wua_year treat_status, ///
					dynamic(22) average_effect robust_dynamic covariances ///
					longdiff_placebo placebo(12) jointtestplacebo breps(100) cluster(wr_group) ///
					controls(jan_april_mean_ppt may_sep_mean_ppt jan_april_mean_et0_elev may_sep_mean_et0_elev) ///
					trends_nonparam(init_af_quint) ///
					save_results(`y'_didl_100breps)
					
}	

********************************************************************************
***************** Center Pivot to LEPA *****************************************
********************************************************************************

import delimited using "${dr_temp}\cp_lepa_justIRR_attgt_prepped.csv", clear
rename wr_group WR_GROUP
rename jan_april_mean_et0_elev jan_april_mean_ET0_elev
rename may_sep_mean_et0_elev may_sep_mean_ET0_elev
	*Compare to two-way fixed effects - 1996 to 2005
	keep if inrange(wua_year, 1996, 2005)
	xtset WR_GROUP wua_year
	xtdescribe
		putexcel set "$dr_output_tables\twfe_v2.xlsx", ///
			sheet("cp to lepa_1996 2005") modify
		local i = 3
		foreach y in "af_used_irr" "af_used"  "acres_irr" "depth_applied" {
			putexcel A`i' = "`y'"
			putexcel B`i' = "effect_average"
			sum `y'
			local var_mean = r(mean)
				putexcel H`i' = `var_mean'
			xtreg `y' treat_status ///
				jan_april_mean_ppt may_sep_mean_ppt jan_april_mean_ET0_elev may_sep_mean_ET0_elev i.wua_year ///
				, fe vce(cluster WR_GROUP)
			putexcel C`i' = _b[treat_status]
			putexcel D`i' = _se[treat_status]
			local ++i
		}

	*Now do event study
	gen t_to = wua_year - cohort_cp_lepa  // Create variable showing time relative to treatment for eventdd
	replace t_to = . if num_switch_cp_lepa==0
	
	putexcel set "$dr_output_tables\event_study_twfe_v2.xlsx", ///
		sheet("cp to lepa_1996 2005") modify
	local i = 0
	foreach y in "af_used_irr" "af_used"  "acres_irr" "depth_applied" {
		local starter = 2 + `i'*37
		local ender = `starter' + 23
		putexcel F`starter' = "`y'"
		sum `y'
		local var_mean = r(mean)
			putexcel G`starter' = `var_mean'
			qui eventdd `y' jan_april_mean_ppt may_sep_mean_ppt jan_april_mean_ET0_elev may_sep_mean_ET0_elev i.wua_year, timevar(t_to) method(fe, cluster(WR_GROUP)) graph_op(leg(off))		
		putexcel A`starter' = matrix(e(leads))
		putexcel A`ender' = matrix(e(lags))
		local ++i
	}		
		
	************************************DYNAMIC - CP to CP with LEPA *************************************
	import delimited using "${dr_temp}\cp_lepa_justIRR_attgt_prepped.csv", clear
	xtile init_af_quint = init_af_used, nq(5)
	
	cd "$dr_output_tables"
	foreach y in "af_used"  "acres_irr" "depth_applied" {
		did_multiplegt `y' wr_group wua_year treat_status, ///
						dynamic(22) average_effect robust_dynamic covariances ///
						longdiff_placebo placebo(14) jointtestplacebo breps(100) cluster(wr_group) ///
						controls(jan_april_mean_ppt may_sep_mean_ppt jan_april_mean_et0_elev may_sep_mean_et0_elev) ///
						trends_nonparam(init_af_quint) ///
						save_results(cp_to_lepa_`y'_didl_100breps)
						
	}